package handle

import (
	"protocx/entity"
	"protocx/protobufx"

	"github.com/gofiber/fiber/v2"
)

func NewResp() Resp {
	return Resp{}
}

type Resp struct {
}

func (r Resp) InitRoute(server *fiber.App) {
	p := server.Group("/resp")
	p.Get("/string", r.String())
	p.Get("/json", r.Json())
	p.Get("/jsonp", r.JSONP())
	p.Get("/xml", r.XML())
	p.Get("/binary", r.Binary())
	p.Get("/markdown", r.Markdown())
	p.Get("/html", r.HTML())
	p.Get("/protobuf", r.Protobuf())
}
func (r Resp) String() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		_, err := ctx.WriteString("string")
		if err != nil {
			return err
		}
		return nil
	}
}
func (r Resp) Json() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return ctx.JSON(entity.Resp{
			Code: 200,
			Msg:  "JSON",
			Data: nil,
		})
	}
}
func (r Resp) JSONP() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return ctx.JSONP(&protobufx.LoginRequest{
			Code: 879,
			Msg:  "JSONP",
		})
	}
}
func (r Resp) XML() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return ctx.XML(entity.Resp{
			Code: 200,
			Msg:  "XML",
			Data: []string{"a", "b"},
		})
	}
}
func (r Resp) Binary() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		_, err := ctx.Write([]byte("abcd"))
		if err != nil {
			return err
		}
		return nil
	}
}
func (r Resp) Markdown() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return nil
	}
}
func (r Resp) TextIndentedJSON() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return nil
	}
}
func (r Resp) HTML() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return nil
	}
}
func (r Resp) Protobuf() fiber.Handler {
	return func(ctx *fiber.Ctx) error {
		return nil
	}
}
